package com.admin.controller;

import com.data.entity.${table.className};
import com.admin.service.${table.className}Service;
import com.admin.util.CommonUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* ${table.className}控制器
* @author wangghua
*/
@RestController
@RequestMapping("/${table.lowerClassName}")
@Api(tags = "${table.className}控制器")
public class ${table.className}Controller  {
    private Logger logger = LogUtil.getBussinessLogger();
    @Resource
    private ${table.className}Service ${table.lowerClassName}Service;


    /**
    * 获取分页列表
    * @param limit 页码
    * @param page 每页显示条数
    * @return
    */
    @GetMapping("/listPage")
    @ApiOperation("分页列表")
    public Result list(Integer limit, Integer page) {
        Result result = new Result();
        try {
            List<${table.className}> ${table.lowerClassName}List = ${table.lowerClassName}Service.listPage(page, limit);
            result.setCode(Result.Code.SUCCESS);
            result.setData(${table.lowerClassName}List);
            result.setCount(${table.lowerClassName}Service.count());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
            return result;
    }

    /**
    * 获取信息
    * @param id
    * @return
    */
    @ApiOperation("获取信息")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        Result result = new Result();
        try {
            ${table.className} ${table.lowerClassName} = ${table.lowerClassName}Service.getInfo(id);
            result.setCode(Result.Code.SUCCESS);
            result.setData(${table.lowerClassName});
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }

            return result;
    }


    /**
    * 添加或修改
    * @param ${table.lowerClassName}
    * @return
    */
    @ApiOperation("添加或修改")
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(${table.className} ${table.lowerClassName}) {
        Result result = new Result();
        try {
            if (!CommonUtil.isEmpty(${table.lowerClassName}.getId())){
            //修改信息
                ${table.className} ${table.lowerClassName}1 = ${table.lowerClassName}Service.getInfo(${table.lowerClassName}.getId());
                ${table.lowerClassName}1=${table.lowerClassName};
                ${table.lowerClassName}1.setUpdatetime(new Date());  //更新
                ${table.lowerClassName}Service.edit(${table.lowerClassName}1);
                result.setMessage(Result.Code.SUCCESS, "操作成功！");
            } else {
                ${table.lowerClassName}Service.add(${table.lowerClassName});
                result.setMessage(Result.Code.SUCCESS, "添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }


    /**
    * 删除信息
    * @param id
    * @return
    */
    @ApiOperation("删除信息")
    @GetMapping("/delete")
    public Result delete(Long id){
        Result result=new Result();
        try {
            ${table.className} ${table.lowerClassName} = ${table.lowerClassName}Service.getInfo(id);
            ${table.lowerClassName}Service.delete(id);
            result.setCode(Result.Code.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setCode(Result.Code.SYSTEMERROR);
        }
         return result;
    }



}
